package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.SetHeartRateActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.model.LoginModel;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Timmy
 * @Data: 2017/7/18
 * @Fuction:
 */

public class SetHeartRateActivityPresenter extends BasePresenter<LoginModel> {

    private SetHeartRateActivity mRateActivity;
    private Map<String, String> mMap;

    private int requestMode = 0;

    public SetHeartRateActivityPresenter(SetHeartRateActivity heartRateActivity) {
        super(LoginModel.class);
        mRateActivity = heartRateActivity;
    }

    @Override
    public void setRequestInfo() {

    }

    @Override
    protected Map<String, String> getData() {
        return mMap;
    }

    @Override
    protected void parsrData(LoginModel loginModel) {
        int code = Integer.parseInt(loginModel.getCode());
        if (code == 200) {
            saveUserData(loginModel.getData());
        }
    }

    // 设置心率是否报警
    public void setRequestIsAlarm(int alarmState) {
        requestMode = 1;
        mMap = new HashMap<>();
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap.put("isalarm", String.valueOf(alarmState));
        setBaseOkgo("user/update-user-info");
    }

    // 保存新的用户数据
    private void saveUserData(LogInDataBean data) {
        // 保存到内存当中
        MyApplication.mLogInDataBean = data;

        data.setUid(1);
        // 将数据存储到数据库中
        mRealm.beginTransaction();
        mRealm.copyToRealmOrUpdate(data);
        mRealm.commitTransaction();
    }

    public void setRequestHrAlarm(String currentItem) {
        requestMode = 2;
        mMap = new HashMap<>();
        mMap.put("username", MyApplication.mLogInDataBean.getUsername());
        mMap.put("hr_alarm", currentItem);
        setBaseOkgo("user/update-user-info");
    }
}
